package com.okhttp.comment;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.baselibrary.bean.BottomDialogBean;
import com.baselibrary.utils.CommonUtil;
import com.baselibrary.utils.NumberUtils;
import com.baselibrary.utils.PopUtils;
import com.baselibrary.utils.ScreenUtils;
import com.baselibrary.utils.SpUtils;
import com.baselibrary.utils.TimeUtil;
import com.baselibrary.utils.ToastUtils;
import com.baselibrary.view.VipHeardImgView;
import com.like.LikeButton;
import com.like.OnLikeListener;
import com.okhttp.R;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 关忆北丶
 * @description:
 * @date :2021/3/25 17:24
 * <p>
 * 精华 或者 普通 评论 界面适配
 */
public class BaseCommentAdapter extends RecyclerView.Adapter<BaseCommentAdapter.BaseCommentHolder> {

    private List<CommentBean.DataBean.CommentInfoBean> mNorMarListBeans = new ArrayList<>();
    private Context mContext;
    private OnCommentClickListener mOnCommentClickListener;
    private CommentBean.DataBean mCommentBean;
    private Activity activity;
    private BaseCommentChildListAdapter lastChildListAdapter;


    public BaseCommentAdapter(Activity activity, OnCommentClickListener mOnCommentClickListener) {
        this.activity = activity;
        this.mOnCommentClickListener = mOnCommentClickListener;
    }

    public List<CommentBean.DataBean.CommentInfoBean> getData() {
        return mNorMarListBeans;
    }

    @SuppressLint("NotifyDataSetChanged")
    public void setNorMarListBeans(List<CommentBean.DataBean.CommentInfoBean> mNorMarListBeans) {
        this.mNorMarListBeans = mNorMarListBeans;
        this.notifyDataSetChanged();
    }

    @SuppressLint("NotifyDataSetChanged")
    public void addNorMarListBeans(List<CommentBean.DataBean.CommentInfoBean> mNorMarListBeans) {
        this.mNorMarListBeans.addAll(mNorMarListBeans);
        this.notifyDataSetChanged();
    }

    public void setCommentBean(CommentBean.DataBean mCommentBean) {
        this.mCommentBean = mCommentBean;
    }

    /**
     * 本地添加一个新的一级评论
     */
    @SuppressLint("NotifyDataSetChanged")
    public void addNewCommentBeans(CommentBean.DataBean.CommentInfoBean commentInfoBean) {
        this.mNorMarListBeans.add(0, commentInfoBean);
        notifyDataSetChanged();
    }

    /**
     * 本地添加一个新的 二级评论。。
     */
    public void addNewChildrenCommentBeans(CommentBean.DataBean.CommentInfoBean.ReplyCommentsBean replyCommentsBean, int commentType, int pos) {
        if (lastChildListAdapter != null) {
            lastChildListAdapter.addList(replyCommentsBean);
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    public void removeData(CommentBean.DataBean.CommentInfoBean norMarListBean) {
        mNorMarListBeans.remove(norMarListBean);
        mCommentBean.setCount(mCommentBean.getCount() - 1);
        setCommentBean(mCommentBean);
        notifyDataSetChanged();
    }

    @SuppressLint("NotifyDataSetChanged")
    public void removeData(int pos) {
        if (mNorMarListBeans.size() > pos) {
            mNorMarListBeans.remove(pos);
            mCommentBean.setCount(mCommentBean.getCount() - 1);
            setCommentBean(mCommentBean);
            notifyDataSetChanged();
        }
    }

    /**
     * 替换一条数据
     *
     * @param pos
     */
    @SuppressLint("NotifyDataSetChanged")
    public void replaceData(int pos, CommentBean.DataBean.CommentInfoBean commentInfoBean) {
        if (mNorMarListBeans.size() > pos) {
            mNorMarListBeans.set(pos, commentInfoBean);
            notifyDataSetChanged();
        }
    }


    @NonNull
    @Override
    public BaseCommentHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        mContext = viewGroup.getContext();
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.item_base_comment_list, viewGroup, false);
        return new BaseCommentHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull BaseCommentHolder baseCommentHolder, int pos) {
        CommentBean.DataBean.CommentInfoBean mNorMarListBean = mNorMarListBeans.get(pos);
        baseCommentHolder.setView(mNorMarListBean, pos);
        baseCommentHolder.initListener(mNorMarListBean, pos);
        if (mNorMarListBean.highlight == CommentBean.TRUE) {
            baseCommentHolder.mViewCommentSubject.setVisibility(View.VISIBLE);
            BusinessLayout.setCommentProminent(baseCommentHolder.mViewCommentSubject);
            mNorMarListBean.highlight = CommentBean.FALSE;
        } else {
            baseCommentHolder.mViewCommentSubject.setVisibility(View.GONE);
        }
        baseCommentHolder.mViewLine.setVisibility(mNorMarListBeans.size() - 1 == pos ? View.GONE : View.VISIBLE);
        baseCommentHolder.mTvCommentUserName.post(() -> {
            int marginSum = ScreenUtils.marginSum(activity, 14, 9, 12, 6, 14, 6);
            int userNameMaxWidth = ScreenUtils.getUserNameMaxWidth(activity, marginSum, baseCommentHolder.mIvCommentUserIcon, baseCommentHolder.tvCommentYouFollow, baseCommentHolder.mLlCommentGive);
            baseCommentHolder.mTvCommentUserName.setMaxWidth(userNameMaxWidth);
        });


    }

    @Override
    public int getItemCount() {
        return mNorMarListBeans.size();
    }

    public class BaseCommentHolder extends RecyclerView.ViewHolder {
        private LinearLayout mLlReplyCommentView, mLlCommentGive, mLlSecondLevelComment;
        private LikeButton mIvCommentGive;
        private VipHeardImgView mIvCommentUserIcon;
        private TextView mTvCommentUserName, tvCommentYouFollow, mTvCommentContent, tvCommentAuthorGive, mTvCommentReplyNum, mTvCommentTime, mTvCommentGiveSum, mTvCommentEnd;
        private RecyclerView mRvCommentReply;
        private BaseCommentChildListAdapter commentChildListAdapter;
        private View mViewLine, mViewCommentSubject;
        private ProgressBar mPbCommentLoad;

        public BaseCommentHolder(@NonNull View itemView) {
            super(itemView);
            mViewCommentSubject = itemView.findViewById(R.id.view_comment_subject);
            mIvCommentUserIcon = itemView.findViewById(R.id.iv_comment_user_icon);
            mTvCommentUserName = itemView.findViewById(R.id.tv_comment_user_name);
            tvCommentYouFollow = itemView.findViewById(R.id.tv_comment_youFollow);
            mTvCommentContent = itemView.findViewById(R.id.tv_comment_content);
            mLlReplyCommentView = itemView.findViewById(R.id.ll_reply_comment_view);
            mRvCommentReply = itemView.findViewById(R.id.rv_comment_reply);
            mTvCommentReplyNum = itemView.findViewById(R.id.tv_comment_replyNum);
            mTvCommentTime = itemView.findViewById(R.id.tv_comment_time);
            mLlCommentGive = itemView.findViewById(R.id.ll_comment_give);
            mLlSecondLevelComment = itemView.findViewById(R.id.ll_secondLevel_comment);
            mIvCommentGive = itemView.findViewById(R.id.iv_comment_give);
            mTvCommentGiveSum = itemView.findViewById(R.id.tv_comment_giveSum);
            mTvCommentEnd = itemView.findViewById(R.id.tv_comment_end);
            mPbCommentLoad = itemView.findViewById(R.id.pb_comment_load);
            mViewLine = itemView.findViewById(R.id.view_line);
            tvCommentAuthorGive = itemView.findViewById(R.id.tv_comment_authorGive);
        }

        @SuppressLint("SetTextI18n")
        public void setView(CommentBean.DataBean.CommentInfoBean norMarListBean, int pos) {
            if (norMarListBean == null)
                return;
            CommentBean.DataBean.CommentInfoBean.UserBean mUser = norMarListBean.getUser();
            String mCueContent = norMarListBean.getCueContent();
            if (mUser != null) {
                mIvCommentUserIcon.setParams((int) activity.getResources().getDimension(R.dimen.dp_36), (int) activity.getResources().getDimension(R.dimen.dp_36), (int) activity.getResources().getDimension(R.dimen.dp_18), (int) activity.getResources().getDimension(R.dimen.dp_14));
                mIvCommentUserIcon.setData(mUser.getIcon(), mUser.getIsFans() == 1, View.GONE);
                mTvCommentUserName.setText(TextUtils.isEmpty(mCueContent) ? mUser.getName() : mUser.getName() + "回复 " + mCueContent);
                RelativeLayout.LayoutParams userNameParams = (RelativeLayout.LayoutParams) mTvCommentUserName.getLayoutParams();
                LinearLayout.LayoutParams followParams = (LinearLayout.LayoutParams) tvCommentYouFollow.getLayoutParams();
                if (mUser.getIsFans() == 1) {
                    userNameParams.topMargin = ScreenUtils.dip2px(activity, 16f);
                    followParams.topMargin = ScreenUtils.dip2px(activity, 16f);
                } else {
                    userNameParams.topMargin = ScreenUtils.dip2px(activity, 8f);
                    followParams.topMargin = ScreenUtils.dip2px(activity, 8f);
                }
                mTvCommentUserName.setLayoutParams(userNameParams);
                tvCommentYouFollow.setLayoutParams(followParams);
            }
            //作者是否赞过
            tvCommentAuthorGive.setVisibility(norMarListBean.authorLike == CommentBean.TRUE ? View.VISIBLE : View.GONE);


            if (norMarListBean.isAuthor == CommentBean.TRUE) {
                tvCommentYouFollow.setText("作者");
                tvCommentYouFollow.setVisibility(View.VISIBLE);
            } else if (mUser != null && (mUser.isFocusFans() == CommentBean.FOLLOW_HE || mUser.isFocusFans() == CommentBean.FOLLOW_MUTUAL)) {
                tvCommentYouFollow.setText("你的关注");
                tvCommentYouFollow.setVisibility(View.VISIBLE);
            } else {
                tvCommentYouFollow.setVisibility(View.GONE);
            }

            mTvCommentTime.setText(TimeUtil.fromToday(norMarListBean.getCtime()));
            mIvCommentGive.setLiked(norMarListBean.isSupported() == CommentBean.TRUE);
            mTvCommentContent.setText(norMarListBean.getContent());
            mTvCommentGiveSum.setText(NumberUtils.formatBigNum(String.valueOf(norMarListBean.getSupportNum())));
            mTvCommentGiveSum.setVisibility(norMarListBean.getSupportNum() > 0 ? View.VISIBLE : View.INVISIBLE);
            if (norMarListBean.getReplyCount() > 2) {
                mLlReplyCommentView.setVisibility(View.VISIBLE);
                if (norMarListBean.getReplyComments() != null)
                    mTvCommentReplyNum.setText("展开" + (norMarListBean.getReplyCount() - norMarListBean.getReplyComments().size()) + "条回复");
            } else {
                mLlReplyCommentView.setVisibility(View.GONE);
            }
            List<CommentBean.DataBean.CommentInfoBean.ReplyCommentsBean> mReplyComments = norMarListBean.getReplyComments();
            commentChildListAdapter = new BaseCommentChildListAdapter(activity);
            commentChildListAdapter.setMorMarListBean(norMarListBean);
            commentChildListAdapter.setList(mReplyComments);
            commentChildListAdapter.setOnCommentChildClickListener(new OnCommentChildClickListener() {
                @Override
                public void onCommentChildUsrHeadClick(CommentBean.DataBean.CommentInfoBean.ReplyCommentsBean replyCommentsBean) {
                    if (mOnCommentClickListener != null) {
                        mOnCommentClickListener.onUserHead(replyCommentsBean);
                    }
                }

                @Override
                public void onCommentChildClick(CommentBean.DataBean.CommentInfoBean.ReplyCommentsBean replyCommentsBean) {
                    if (mOnCommentClickListener != null) {
                        lastChildListAdapter = commentChildListAdapter;
                        mOnCommentClickListener.onCommentChildClick(norMarListBean, replyCommentsBean, commentChildListAdapter, pos);
                    }
                }

                @Override
                public void onCommentChildGive(CommentBean.DataBean.CommentInfoBean.ReplyCommentsBean replyCommentsBean, TextView mSupportNum, LikeButton ivCommentGive) {
                    if (mOnCommentClickListener != null) {//子评论点赞
                        mOnCommentClickListener.onGiveReplyComment(replyCommentsBean, mSupportNum, ivCommentGive);
                    }
                }

                @Override
                public void onCommentChildDelete(CommentBean.DataBean.CommentInfoBean.ReplyCommentsBean replyCommentsBean, int childPos) {
                    if (mOnCommentClickListener != null) {
                        mOnCommentClickListener.onDeleteReplyComment(norMarListBean, replyCommentsBean, commentChildListAdapter, mTvCommentReplyNum, childPos, pos);
                    }
                }

                @Override
                public void onReportChildComment(CommentBean.DataBean.CommentInfoBean.ReplyCommentsBean replyCommentsBean) {
                    if (mOnCommentClickListener != null) {
                        mOnCommentClickListener.onReportChildComment(replyCommentsBean);
                    }
                }

                @Override
                public void onCopyChildCommentContent(CommentBean.DataBean.CommentInfoBean.ReplyCommentsBean replyCommentsBean) {
                    if (!TextUtils.isEmpty(replyCommentsBean.getContent())) {
                        CommonUtil.contentClipboard(activity.getApplicationContext(), replyCommentsBean.getContent(), "复制成功");
                    } else {
                        ToastUtils.showShort(activity.getApplicationContext(), "复制出错或没有可复制内容～");
                    }
                }
            });
            mRvCommentReply.setAdapter(commentChildListAdapter);
            if (mReplyComments != null && mReplyComments.size() > 0) {
                mRvCommentReply.setVisibility(View.VISIBLE);
            } else {
                mRvCommentReply.setVisibility(View.GONE);
            }
        }

        public void initListener(CommentBean.DataBean.CommentInfoBean norMarListBean, int pos) {
            itemView.setOnLongClickListener(v -> {
                if (mOnCommentClickListener == null)
                    return true;
                StringBuilder buffer = new StringBuilder();
                String content = norMarListBean.getContent();
                buffer.append(norMarListBean.getUser().getName());
                if (!TextUtils.isEmpty(content)) {
                    buffer.append("：")
                            .append(content.length() > 12 ? content.substring(0, 12) : content);
                }
                ArrayList<BottomDialogBean> list = new ArrayList<>();
                list.add(new BottomDialogBean("回复", false));
                list.add(new BottomDialogBean("复制", false));
                if (SpUtils.getInstance().getIsLogin() && SpUtils.getInstance().getUserId() == norMarListBean.getUser().getId()) {
                    list.add(new BottomDialogBean("删除", true));
                } else {
                    list.add(new BottomDialogBean("举报", false));
                }
                PopUtils.INSTANCE.showBottomDialog(activity, buffer.toString(), list, pos1 -> {
                    switch (pos1) {
                        case 0:
                            mOnCommentClickListener.onCommentView(norMarListBean, pos);
                            lastChildListAdapter = commentChildListAdapter;
                            break;
                        case 1:
                            mOnCommentClickListener.onCopyCommentContent(norMarListBean);
                            break;
                        case 2:
                            if (SpUtils.getInstance().getIsLogin() && SpUtils.getInstance().getUserId() == norMarListBean.getUser().getId()) {
                                mOnCommentClickListener.onDeleteComment(norMarListBean);
                            } else {
                                mOnCommentClickListener.onReportComment(norMarListBean);
                            }
                            break;
                    }
                },true);

                return true;
            });


            itemView.setOnClickListener(view -> {
                if (mOnCommentClickListener == null)
                    return;
                mOnCommentClickListener.onCommentView(norMarListBean, pos);
                lastChildListAdapter = commentChildListAdapter;
            });

            mIvCommentUserIcon.setOnClickListener(v -> {
                if (mOnCommentClickListener != null) {
                    mOnCommentClickListener.onUserHead(norMarListBean);
                }
            });
            mIvCommentGive.setOnLikeListener(new OnLikeListener() {
                @Override
                public void liked(LikeButton likeButton) {
                    onClickGive(norMarListBean);
                }

                @Override
                public void unLiked(LikeButton likeButton) {
                    onClickGive(norMarListBean);
                }
            });
            mLlReplyCommentView.setOnClickListener(v -> {
                if (mOnCommentClickListener != null) {
                    mPbCommentLoad.setVisibility(View.VISIBLE);
                    mOnCommentClickListener.onLookCommentDetails(norMarListBean, commentChildListAdapter, mPbCommentLoad, new OnLookSurplusCommentListener() {
                        @Override
                        public void onLookSurplusComment(int loadCount) {
                            int count = norMarListBean.getReplyCount() - commentChildListAdapter.mList.size();
                            if (count > 0) {
                                mLlReplyCommentView.setVisibility(View.VISIBLE);
                                mTvCommentReplyNum.setText("展开" + count + "条回复");
                            } else {
                                mLlReplyCommentView.setVisibility(View.GONE);
                            }

                        }
                    });
                }
            });
        }

        private void onClickGive(CommentBean.DataBean.CommentInfoBean norMarListBean) {
            if (mOnCommentClickListener != null) {
                mOnCommentClickListener.onGiveComment(norMarListBean, mIvCommentGive, mTvCommentGiveSum);
                if (norMarListBean.getAuthUser() != null) {
                    if (SpUtils.getInstance().getLoadUserBean().getData().getId() == norMarListBean.getAuthUser().getId()) {
                        norMarListBean.authorLike = norMarListBean.authorLike == CommentBean.FALSE ? CommentBean.TRUE : CommentBean.FALSE;
                        //作者是否赞过
                        tvCommentAuthorGive.setVisibility(norMarListBean.authorLike == CommentBean.TRUE ? View.VISIBLE : View.GONE);
                    }
                }
            }
        }
    }
}
